package org.jslaughter.component.inventory;

import org.nvframe.component.render.MultipleImageRender;
import org.nvframe.component.state.Position;
import org.nvframe.entity.Entity;
import org.nvframe.entity.EntityImpl;
import org.nvframe.event.EventService;
import org.nvframe.event.eventtype.UpdateEvent;
import org.nvframe.event.eventtype.UpdateListener;
import org.nvframe.util.settings.SettingsObj;

public class Weapon extends EntityImpl implements UpdateListener {

	private Entity weaponOwner = null;
	private String followRenderDirection = null;
	private int weaponTargetSlot;
	
	public Weapon(String id, String name, SettingsObj settings) {
		super(id, name);
		
		weaponTargetSlot = settings.getInt("slot", 1);
		followRenderDirection = settings.getString("followRenderDirection", null);
		
		EventService.getInstance().addEventListener(this);
	}
	
	/**
	 * set the inventory owner entity for easy communication 
	 * between the inventory's owner and the inventory items
	 * 
	 * @param inventoryOwner
	 */
	public void setWeaponOwner(Entity owner) {
		this.weaponOwner = owner;
	}
	
	public int getWeaponTargetSlot() {
		return weaponTargetSlot;
	}

	public void setWeaponTargetSlot(int weaponTargetSlot) {
		this.weaponTargetSlot = weaponTargetSlot;
	}

	@Override
	public void onUpdate(UpdateEvent event) {
		/*if(getComponentCount() > 0 && !hasComponent(Position.class))
			EventService.getInstance().removeEventListener(this);*/
		
		Position ownerPos = (Position) getComponent(Position.class);
		Position invOwnerPos = (Position) weaponOwner.getComponent(Position.class);
		
		if(invOwnerPos == null)
			return;

		// follow owner's position
		ownerPos.setXY(invOwnerPos.getXY());

		if(followRenderDirection == null)
			ownerPos.setDirection(invOwnerPos.getDirection());
		else {
			MultipleImageRender ownerRender = (MultipleImageRender) weaponOwner.getComponent(MultipleImageRender.class);
			ownerPos.setDirection(ownerRender.getDirection(followRenderDirection));
		}
	}

}
